﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace CardPlay.Model
{
    public class LocalizedDescriptionAttribute : DescriptionAttribute
    {
        private PropertyInfo _nameProperty;
        private Type _resourceType;

        public LocalizedDescriptionAttribute(string displayNameKey) : base(displayNameKey)
        {
        }

        public Type NameResourceType
        {
            get
            {
                return _resourceType;
            }
            set
            {
                _resourceType = value;
                _nameProperty = _resourceType.GetProperty(base.Description, BindingFlags.Static | BindingFlags.NonPublic);
            }
        }

        public override string Description
        {
            get
            {
                //check if nameProperty is null and return original display name value
                return _nameProperty == null
                    ? base.Description
                    : (string) _nameProperty.GetValue(_nameProperty.DeclaringType, null);
            }
        }
    }
}